package com.tust.service;

import com.tust.bean.Student;
import com.tust.dao.StudentDao;

import java.sql.Connection;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class StudentServiceImpl implements BaseService{
    // 学生表业务逻辑
    public void service(Connection connection) {

        Scanner scanner = new Scanner(System.in);
        String type;

        List<Student> list;

        while (true) {
            System.out.println("请输入学生管理操作命令：add->添加，edit->编辑,del->删除,Query->查询，QUIT->退出");
            StudentDao studentDao = new StudentDao(connection);
            type = scanner.next();

            if (type.equalsIgnoreCase("add")) {
                System.out.println("请依次单独、学生ID、姓名、年龄、电话：");
                Student student = new Student();
                student.setSid(scanner.nextInt());
                student.setSname(scanner.next());
                student.setSage(scanner.nextInt());
                student.setSphone(scanner.next());
                if (studentDao.insert(student)) {
                    System.out.println("添加成功");
                    list = studentDao.query();
                    print(list);
                } else {
                    System.out.println("添加失败");
                }
            } else if (type.equalsIgnoreCase("del")) {
                // 打印学生信息
                list = studentDao.query();
                print(list);

                System.out.println("删除指定学生请输入学生号，删除所有学生输入all。");
                type = scanner.next();
                if (type.equalsIgnoreCase("all")) {
                    if (studentDao.delete()) {
                        System.out.println("所有学生记录删除成功！");
                    } else {
                        System.out.println("所有学生记录删除失败！");
                    }
                } else {
                    if (studentDao.delete(Integer.parseInt(type))) {
                        System.out.println(type + "学生删除成功！");
                        list = studentDao.query();
                        print(list);
                    } else {
                        System.out.println(type + "学生删除失败！");
                    }
                }
            } else if (type.equalsIgnoreCase("edit")) {
                // 打印学生列表
                list = studentDao.query();
                print(list);

                // 更新学生信息
                System.out.println("请依次单独输入要更新学生的学生号、姓名、年龄、电话：");
                Student student = new Student();
                student.setSid(scanner.nextInt());
                student.setSname(scanner.next());
                student.setSage(scanner.nextInt());
                student.setSphone(scanner.next());
                if (studentDao.update(student)) {
                    System.out.println("更新成功");
                    list = studentDao.query();
                    print(list);
                } else {
                    System.out.println("更新失败");
                }
            } else if (type.equalsIgnoreCase("query")) {
                System.out.println("查询指定学生请输入学生号，查询所有学生输入all。");
                type = scanner.next();
                if (type.equalsIgnoreCase("all")) {
                    list = studentDao.query();
                    print(list);

                } else {
                    Student student = studentDao.query(Integer.parseInt(type));
                    if (student != null) {
                        System.out.println(student.getSid() + "\t" + student.getSname() + "\t" + student.getSage() + "\t" +student.getSphone());
                    } else {
                        System.out.println("查询的学生号:" + type + "不存在！");
                    }
                }
            } else if (type.equalsIgnoreCase("QUIT")) {
                studentDao.close();
                break;
            }
        }
    }

    @Override
    public void print(List list) {

        if(list.size()>0) {
            Iterator<Student> iterator = list.iterator();
            System.out.println("学号\t\t  姓名\t  年龄\t手机号");
            while (iterator.hasNext()) {
                Student student = iterator.next();
                String sid = String.format("%-10s", student.getSid());
                String sname = String.format("%-6s", student.getSname());
                String sage = String.format("%-6s", student.getSage());
                String sphone = String.format("%-11s", student.getSphone());
                System.out.println(sid + sname + sage + sphone);
            }
        }else{
            System.out.println("无学生记录，请录入！");
        }
    }
}
